
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author leehoa
 */
public class xmlstring {
    static String host = "127.0.0.1";
    static String user = "manager";
    static String pwd  = "123456";
    static String port = "22222";
    static String filename = "infor.properties"; 
    static Properties mprop;
    public static void main(String[] args) {        
        mprop = new Properties();
        try{
                       
            File f = new File(filename);
            if(!f.exists()){
                f.createNewFile();
            }
            FileInputStream fis = new FileInputStream(f);    
            if(fis.available() == 0 ){
                System.out.println("file doesn't exist"); 
                write(filename);
                mprop.setProperty("pwd", "11111111111111");
                mprop.store(new FileOutputStream(filename), null);
                System.out.println("write file"); 
            }
            read(filename);
            System.out.println("read file"); 
            mprop.load(new FileInputStream(filename));
            System.out.println("read file\t"+mprop.getProperty("aaa"));
                     
        }catch(Exception e){
        }

            
    }
    
    static void write(String fname){
        Properties prop = new Properties();
 
        try {
            //set the properties value
            mprop.setProperty("host", host);
            mprop.setProperty("user", user);
            mprop.setProperty("pwd", pwd);
            mprop.setProperty("port", port);
 
            //save properties to project root folder
            mprop.store(new FileOutputStream(fname), null);
 
        } catch (IOException ex) {
            ex.printStackTrace();
        }        
    }
    
    static void read(String fname){
        Properties prop2 = new Properties();
 
        try {
            //load a properties file
            mprop.load(new FileInputStream(fname));

            //get the property value and print it out
            System.out.println(mprop.getProperty("host"));
            System.out.println(mprop.getProperty("user"));
            System.out.println(mprop.getProperty("pwd"));
            System.out.println(mprop.getProperty("port"));
 
        } catch (IOException ex) {
            ex.printStackTrace();
        }        
    }
    
}
